package com.Web.Servlet;

import com.Web.Person.Course;
import com.Web.Service.ICourseService;
import com.Web.Service.Impl.CourseServiceImpl;
import com.Web.util.JDBCUtil;
import com.Web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/course")
public class CourseServlet extends HttpServlet {

    private ICourseService courseService = new CourseServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if(method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectAll":
                selectAll(req,resp);
                break;
            case "selectCourseAddPage":
                selectCourseAddPage(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "toUpdate":
                toUpdate(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.update");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        String name = req.getParameter("name");
        String credits = req.getParameter("credit");
        int credit = Integer.parseInt(credits);
        Course course = new Course(id, name, credit);
        courseService.update(course);
        resp.sendRedirect("/course");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.toUpdate");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        Course course = courseService.toUpdate(id);
        req.setAttribute("course",course);
        req.getRequestDispatcher("course_update.jsp").forward(req,resp);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.deleteCourse");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        courseService.deleteById(id);
        resp.sendRedirect("/course");
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.add");
        String name = req.getParameter("name");
        String credits = req.getParameter("credit");
        int credit = Integer.parseInt(credits);
        Course course = new Course(name, credit);
        courseService.add(course);
        resp.sendRedirect("/course");
    }

    private void selectCourseAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.selectCourseAddPage");
        req.getRequestDispatcher("/course_add.jsp").forward(req,resp);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.selectAll");
        List<Course> list = courseService.selectAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("course_list.jsp").forward(req,resp);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        if(pageNo == null || pageNo.equals("")){
            pageNo = "1";
        }
        String pageSize = req.getParameter("pageSize");
        if(pageSize == null || pageSize.equals("")){
            pageSize = "5";
        }
        PageInfo<Course> pageInfo = courseService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("course_list.jsp").forward(req,resp);
    }
}
